Param(
  [Parameter(Mandatory=$true)] [string]$Path,   # 目标文件夹或其父目录
  [switch]$Recursive,                           # 递归处理子文件夹（每个子文件夹视作一个视频集）
  [int]$Fps = 3,
  [int]$ScaleH = 720,
  [string]$Owner = $env:USERNAME,
  [string]$Location = '',
  [string]$TimeOfDay = '',
  [string]$Weather = '',
  [string]$Notes = ''
)

function Write-Meta($dir) {
  if (-not (Test-Path $dir -PathType Container)) { Write-Warning "跳过（非目录）：$dir"; return }
  $name = Split-Path $dir -Leaf
  $count = (Get-ChildItem -Path $dir -Filter *.jpg -File -ErrorAction SilentlyContinue).Count
  $date = Get-Date -Format 'yyyy-MM-dd'
  $metaPath = Join-Path $dir 'meta.csv'
  $header = "video_id,fps_extract,scale_h,frame_count,owner,date,location,time_of_day,weather,notes"
  $row = "$name,$Fps,$ScaleH,$count,$Owner,$date,$Location,$TimeOfDay,$Weather,$Notes"
  Set-Content -LiteralPath $metaPath -Value ($header + "`n" + $row + "`n") -Encoding UTF8
  Write-Host "生成 meta.csv → $metaPath （帧数=$count）" -ForegroundColor Yellow
}

if ($Recursive) {
  $dirs = Get-ChildItem -Directory -Path $Path -ErrorAction Stop
  if ($dirs.Count -eq 0) { Write-Warning "未发现子目录：$Path" }
  foreach ($d in $dirs) { Write-Meta $d.FullName }
} else {
  Write-Meta $Path
}

Write-Host "完成。meta.csv 已写入对应文件夹。" -ForegroundColor Cyan

